<?php

namespace app\admin\model\yxerp;

use think\Model;

/**
 * Position
 */
class Position extends Model
{
    // 表名
    protected $name = 'yxerp_position';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    
    // 追加属性
    protected $append = [
        'department_name'
    ];
    
    /**
     * 关联部门
     */
    public function department()
    {
        return $this->belongsTo(Department::class, 'department_id', 'id');
    }
    
    /**
     * 获取部门名称
     */
    public function getDepartmentNameAttr($value, $data)
    {
        if (isset($data['department_id'])) {
            $department = Department::where('id', $data['department_id'])->find();
            return $department ? $department['name'] : '';
        }
        return '';
    }
}